/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package biofilter.records;

import biofilter.util.*;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Daniel Quest
 * @date   Feb 22, 2010
 * This file exists for a greped (using grep ">" 454PairAlign.txt >> 454PairAlignGrep.aln)
 * version of the 454 mapping software.
 *
 * Record lines of this type look like:
 * >F5ZOH0Y01D7TWA, 77..1 of 77 and CP000568, 3397475..3397552 of 3843301   (77/78 ident)
 */
public class Mapper454Record implements Record, MappedPosition {
    private String record = null;                //>F5ZOH0Y01D7TWA, 77..1 of 77 and CP000568, 3397475..3397552 of 3843301   (77/78 ident)
    private String readID = null;                //>F5ZOH0Y01D7TWA
    private int readStart = Integer.MIN_VALUE;   //1
    private int readEnd = Integer.MIN_VALUE;     //77
    private char readStrand = '.';               //'-' (because 77 is > 1
    private int readLength = Integer.MIN_VALUE;  //77
    private String genomeID = null;              //CP000568
    private int genomeStart = Integer.MIN_VALUE; //3397475
    private int genomeEnd = Integer.MIN_VALUE;   //3397552
    private char genomeStrand = '.';             //'+' (because 3397475 < 3397552)
    private int genomeLength = Integer.MIN_VALUE;//3843301
    private int matches = Integer.MIN_VALUE;     //77
    private int alignLength = Integer.MIN_VALUE; //78

    public String get() {
        return this.record;
    }

    public void set(String r) throws IllegalArgumentException {
        this.record = r;
    }

    ArrayUtil au = new ArrayUtil();
    public void parse(){
        try {
            Integer i = new Integer("0");
            StringUtil su = new StringUtil();
            String[] commaTokens = su.splitOnDelim(this.record, ",", 3);
            this.readID = commaTokens[0].trim();//F5ZOH0Y01D7TWA
            String[] s1 = su.splitOnSpaces(commaTokens[1].trim(), 5); //77..1 of 77 and CP000568
            String[] n1 = su.splitOnDelim(s1[0], "..", 2);//77..1
            this.readStart = i.parseInt(n1[0]); //77
            this.readEnd = i.parseInt(n1[1]);   //1
            if (this.readStart > this.readEnd ){ //determine the read strand, swap if needed
                int temp = this.readStart;
                this.readStart = this.readEnd;
                this.readEnd = temp;
                this.readStrand = '-';
            }else{
                this.readStrand = '+';
            }
            this.readLength = i.parseInt(s1[2]); //77
            this.genomeID = s1[4].trim();               //CP000568
            String[] s2 = su.splitOnALLWhitepace(commaTokens[2].trim(), 4);//3397475..3397552 of 3843301   (77/78 ident)
            String[] n2 = su.splitOnDelim(s2[0], "..", 2);//3397475..3397552
            this.genomeStart = i.parseInt(n2[0].trim()); //3397475
            this.genomeEnd = i.parseInt(n2[1].trim());   //3397552
            if (this.genomeStart > this.genomeEnd ){ //determine the read strand, swap if needed
                int temp = this.genomeStart;
                this.genomeStart = this.genomeEnd;
                this.genomeEnd = temp;
                this.genomeStrand = '-';
            }else{
                this.genomeStrand = '+';
            }
            String gLen = s2[2].trim(); //3843301 ident)
            if(gLen.endsWith("ident)")) gLen = gLen.substring(0, gLen.length() - 6);
            this.genomeLength = i.parseInt(gLen.trim()); //3843301
            String ident = s2[3].trim(); //(77/78
            if(ident.startsWith("(")) ident = ident.substring(1);
            String[] n3 = su.splitOnDelim(ident, "/", 2);//83/85
            this.matches = i.parseInt(n3[0]);
            this.alignLength = i.parseInt(n3[1]);
            
        } catch (ParseException ex) {
            Logger.getLogger(Mapper454Record.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getStart() {
        if(this.genomeStart == Integer.MIN_VALUE){ parse(); }
        return this.genomeStart;
    }

    public int getEnd() {
        if(this.genomeEnd == Integer.MIN_VALUE){ parse(); }
        return this.genomeEnd;
    }

    public int length() {
        if(this.alignLength == Integer.MIN_VALUE){ parse(); }
        return this.alignLength;
    }

    public char getStrand() {
        if(this.readStrand == '.'){ parse(); }
        if (this.readStrand == '+' && this.genomeStrand == '+') return '+';
        if (this.readStrand == '-' && this.genomeStrand == '+') return '-';
        if (this.readStrand == '+' && this.genomeStrand == '-') return '-';
        if (this.readStrand == '-' && this.genomeStrand == '-') return '+';
        return '.';
    }

    public double getWeight() {
        if(this.matches == Integer.MIN_VALUE){ parse(); }
        double score = 0.0;
        //lets say score is matches over align length.
        score += matches;
        score = score/this.alignLength;
        return score;
    }

    public String getQueryID() {
        if(this.readID == null){ parse(); }
        return this.readID;
    }

    public String getSubjectID() {
        if(this.genomeID == null){ parse(); }
        return this.genomeID;
    }

}
